﻿//using Infrastructure.Utils;
using Infrastructure.Utils;
using Microsoft.EntityFrameworkCore;

namespace Application.Utils
{
    public static class Init
    {
        public static IServiceCollection UseDatabaseMigration(this IServiceCollection services)
        {
            var context = services.BuildServiceProvider().GetRequiredService<JobManagerDbContext>();
            context.Database.Migrate();
            return services;
        }
        public static IApplicationBuilder InitData(this IApplicationBuilder app)
        {
            using var scope = app.ApplicationServices.CreateScope();
            var db = scope.ServiceProvider.GetService<JobManagerDbContext>();
            var config = scope.ServiceProvider.GetService<IConfiguration>();
            var isInit = false;
            #region 创建admin根管理员
            var managerService = scope.ServiceProvider.GetService<IManagerService>();
            var password = config["InitAdminPassword"] ?? "123456";

            var manager = db.SysManager.FirstOrDefault(s => s.LoginCode == "admin");
            if (manager == null)
            {
                manager = new SysManager
                {
                    LoginCode = "admin",
                    UserName = "Admin",
                    Password = Password.Create(password),
                    RoleKey = GlobalData.Role.SupperAdminRoleKey
                };
                db.SysManager.Add(manager);
                db.SaveChanges();
                isInit = true;
                LogHelper.Info("系统初始化：", $"根账号：admin；密码：{password}");
            }
            #endregion

            #region 添加日志
            if (isInit)
            {
                db.SysLogOperation.Add(new SysLogOperation
                {
                    IsDelete = false,
                    OperatorName = "$admin",
                    Title = "系统初始化",
                    Note = "初始化",
                    Api = "app.InitData()"
                });
                db.SaveChanges();
            }
            #endregion

            #region 添加示例任务
            if (isInit)
            {
                db.JobTask.Add(new JobTask
                {
                    IsEnable = true,
                    CreatorId = manager.Id,
                    Title = "每分钟请求一次百度",
                    Cron = "0 0/1 * * * ?",
                    Content = "https://www.baidu.com",
                    Group = "示例",
                    Note = "系统示例",

                    LogRetentionDays = 30
                });
                db.SaveChanges();
            }
            #endregion

            #region 标记服务上线
            var server = new KeyValue
            {
                Key = GlobalData.ServerKey
            };
            db.KeyValue.Add(server);
            db.SaveChanges();
            GlobalData.ServerId = server.Id;
            #endregion

            #region 添加锁
            //var listSetting = new List<KeyValue>();
            //listSetting.Add(new KeyValue { Key = "", Value = "", Type = KeyValueType.setting_notice });
            //listSetting.Add(new KeyValue { Key = "", Value = "", Type = KeyValueType.setting_notice });

            //foreach (var item in resourceList)
            //{
            //    db.DistributedLock.Add(new DistributedLock() { Id = (int)item });
            //    db.SaveChanges();
            //}
            #endregion

            return app;
        }

    }
}
